//
//  JoPaw.swift
//  jocool
//
//  Created by tong on 16/6/11.
//  Copyright © 2016年 zhuxietong. All rights reserved.
//

import Foundation
import UIKit
import Eelay

extension JoPaw
{
    
    public class func paw(fields:JoTextField...,activeAlert:Bool=true) ->(values:[String:Any],validate:Bool){
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: FK.SaveJoFormNotify), object: nil)
        
        var values = [String:AnyObject]()
        for item in fields
        {
            let value = item.getValue(activeAlert: activeAlert)
            if value.validate
            {
                values[value.ID] = value.value as AnyObject?
            }
            else
            {
                return (values,false)
            }
        }
        return (values,true)
    }
    
    public class func allFields(tableView:UITableView) ->[StrField]{
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: FK.SaveJoFormNotify), object: nil)

        var list = NSMutableArray()
        _ = [String:AnyObject]()
        
        let sectionCount = tableView.model.count
        
        if sectionCount > 0
        {
            for i in 0 ..< sectionCount
            {
                let value = tableView.model["section\(i)"]
                if let onesection = value as? NSMutableArray
                {
                    list = list + onesection
                }
            }
        }
        return JoPaw.allFields(list: list)
    }
    
    
    public class func paw(sectionObj model:NSMutableDictionary,activeAlert:Bool = true) -> (values:[String:Any],validate:Bool)
    {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: FK.SaveJoFormNotify), object: nil)
        var list = NSMutableArray()
        _ = [String:AnyObject]()
        
        let sectionCount = model.count
        
        if sectionCount > 0
        {
            for i in 0 ..< sectionCount
            {
                let value = model["section\(i)"]
                if let onesection = value as? NSMutableArray
                {
                    list = list + onesection
                }
            }
        }
        return JoPaw.paw(list: list,activeAlert:activeAlert)
        
    }
    
    public class func paw(tableView:UITableView,activeAlert:Bool = true) -> (values:[String:Any],validate:Bool)
    {
        
        return paw(sectionObj: tableView.model, activeAlert: activeAlert)
        
    }
    
    public class func paw(collectionV:UICollectionView,activeAlert:Bool = true) -> (values:[String:Any],validate:Bool)
    {
        return paw(sectionObj: collectionV.model, activeAlert: activeAlert)
    }
    
    public class func paw(collectionNode:ASCollectionNode,activeAlert:Bool = true) -> (values:[String:Any],validate:Bool)
    {
        return paw(sectionObj: collectionNode.model, activeAlert: activeAlert)
    }
    
    
}

public class JoPaw
{
    
    
    public class func allFields(list:NSMutableArray) ->[StrField]{
        var v_list = [StrField]()
        for item in list
        {
            let dict = item as! NSMutableDictionary

            if let item_dict = dict.object(forKey: FK.field) as? NSMutableDictionary
            {

                if let item_post = item_dict.object(forKey: FK.field) as? StrField
                {
                    item_post._value = item_dict[FK.value,""]
                    if !item_post._hiden
                    {
                        v_list.append(item_post)
                    }
                }
            }
            if let posts = dict.object(forKey: FK.fields) as? NSMutableArray
            {
                for one in posts
                {
                    if let item_post = one as? StrField
                    {
                        if !item_post._hiden
                        {
                            v_list.append(item_post)
                        }
                    }
                    if let item_dict = one as? NSMutableDictionary
                    {
                        if let item_post = item_dict.object(forKey: FK.field) as? StrField
                        {
                            item_post._value = item_dict[FK.value,""]
                            if !item_post._hiden
                            {
                                v_list.append(item_post)
                            }
                        }
                    }
                }
            }
            
        }
        return v_list
    }
    
    public class func paw(list:NSMutableArray,activeAlert:Bool=true) ->(values:[String:Any],validate:Bool)
    {

        let info = JoForm.getValues(inputs: JoPaw.allFields(list: list), activeAlert: activeAlert)
        return info
    }
    
    public class func paw(inputs:[StrField],activeAlert:Bool = true) ->(values:[String:Any],validate:Bool)
    {
        return JoForm.getValues(inputs: inputs, activeAlert: activeAlert)
    }
}


public class JoForm {
    
    public class func getValues(inputs:StrField...,activeAlert:Bool = true) ->(values:[String:Any],validate:Bool)
    {
        var values = [String:Any]()
        for item in inputs
        {
            let value = item.getValue(activeAlert: activeAlert)
            if value.validate
            {
                values[value.ID] = value.value as Any?
            }
            else
            {
                return (values,false)
            }
        }
        
        return (values,true)
    }
    
    public class func getValues(inputs:[StrField],activeAlert:Bool=true) ->(values:[String:Any],validate:Bool)
    {
        var values = [String:Any]()
        for item in inputs
        {
            let value = item.getValue(activeAlert: activeAlert)

            
            if value.validate
            {
                values[value.ID] = value.value as Any?
            }
            else
            {
                return (values,false)
            }
        }
        return (values,true)
    }
    
    public class func getDictValues(inputs:StrField...,activeAlert:Bool=true) ->(values:NSMutableDictionary,validate:Bool)
    {
        let values = NSMutableDictionary()
        for item in inputs
        {
            let value = item.getValue(activeAlert: activeAlert)
            if value.validate
            {
                values[value.ID] = value.value
            }
            else
            {
                return (values,false)
            }
        }
        return (values,true)
    }
}
